package com.djx.controller;

import com.alibaba.excel.EasyExcel;
import com.djx.entity.EasyExcelOrderGoodsVo;
import com.djx.entity.Order;
import com.djx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RequestMapping("/easyExcel")
@RestController
public class OrderController {
    private static final String PATH = "D:\\easyExcel\\";
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public List<Order> easyorder() {
        List<Order> orderList = orderService.findAll();
        return orderList;
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void simpleWrite(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("订单明细", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Order.class).sheet("订单明细表1").doWrite(orderService.findAll());
    }


    @RequestMapping(value = "/download1", method = RequestMethod.GET)
    public void download1(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("订单明细", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), EasyExcelOrderGoodsVo.class).sheet("订单明细表1").doWrite(orderService.findEasyExcelOrderGoods());
    }



}
